<template>
  <el-drawer
      title="自定义事件"
      size="800px"
      :model-value="true"
      :before-close="handleCancel"
  >
    <el-form
        :model="eventData"
        :rules="rules"
        ref="eventData"
        label-width="100px"
    >
      <el-form-item label="事件CODE" prop="propertyKey">
        <el-input
            type="text"
            placeholder="请输入数字、字母、下划线"
            :disabled="isDisable"
            @input="setValue"
            :model-value="eventData.propertyKey">
        </el-input>
      </el-form-item>
      <el-form-item label="事件名称" prop="name">
        <el-input
            type="text"
            placeholder="请输入事件名称"
            v-model="eventData.name"
            maxlength="40"
            show-word-limit
        >
        </el-input>
      </el-form-item>


      <!--        <el-form-item label="事件分类" prop="typeId">-->
      <!--          <el-select v-model="eventData.typeId" placeholder="请选择事件分类">-->
      <!--            <el-option-->
      <!--                v-for="item in typeList"-->
      <!--                :key="item.id"-->
      <!--                :label="item.name"-->
      <!--                :value="item.id"-->
      <!--            >-->
      <!--            </el-option>-->
      <!--          </el-select>-->
      <!--        </el-form-item>-->

      <el-form-item label="类型" prop="propertyType">
        <el-select v-model="eventData.propertyType" placeholder="请选择类型" prop="propertyType">
          <el-option
              v-for="item in propertyTypeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="设置快捷入口" prop="showInCanvas">
        <el-switch
          v-model="eventData.showInCanvas"
          :active-value="1"
          :inactive-value="0"
        >
        </el-switch>
      </el-form-item> -->
      <!--      <el-form-item label="选择组件icon">-->
      <!--        <el-tag-->
      <!--            v-for="(item, idx) in iconList"-->
      <!--            :key="idx"-->
      <!--            :class="{ 'tag-active': index == idx }"-->
      <!--            @click="setIcon(item, idx)"-->
      <!--        >-->
      <!--          <i :class="item"></i>-->
      <!--        </el-tag>-->
      <!--      </el-form-item>-->
    </el-form>
    <div class="form-footer">
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </div>
  </el-drawer>

</template>

<script>
import EventApi from "@/api/eventApi";
import {mapMutations} from "vuex";

export default {
  props: {
    data: {
      require: false,
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  emits: ['cancel', 'confirm', 'confirm-edit'],
  data() {
    return {
      isDialog: true,
      isDisable: false,
      typeList: [],
      index: '',
      eventData: {
        name: "",
        typeId: '',
        icon: '',
        event: 'event',
        propertyKey: "",
        showInCanvas: 1,
        propertyType: "",
        historyPropertyKey: '',
      },
      propertyTypeList: [
        {id: 0, name: "数字"},
        {id: 1, name: "字符串"},
        {id: 2, name: "时间"},
        {id: 3, name: "日期"},
      ],
      iconList: [
        "el-icon-tickets",
        "el-icon-document-checked",
        "el-icon-attract",
        "el-icon-data-analysis",
      ],
      rules: {
        propertyKey: [{
          required: true,
          message: '请输入事件CODE',
          trigger: ['blur'],
        }],
        name: [{
          required: true,
          message: "请输入流程名称",
          trigger: ["blur", "change"],
        }],
        propertyType: [{
          required: true,
          message: "请选择类型",
          trigger: ["blur", "change"],
        }],
      },
    };
  },
  created() {
    //debugger
    if(JSON.stringify(this.data) !== '{}') {
      this.isDisable = true
      this.eventData = this.data
      this.eventData.historyPropertyKey = this.data.propertyKey

    }
    this.getSimpleList();
  },
  methods: {
    ...mapMutations("event", ["setPushEventEvent"]),
    formValidate() {
      this.$refs.eventData.validate((valid) => {
        if (valid) {
          if (this.eventData.publishTime && this.eventData.invalidTime) {
            this.active++;
          } else {
            this.$message.warning("请输入时间");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },


    setIcon(item, idx) {
      this.eventData.icon = item
      this.index = idx
    },

    async getSimpleList() {
      let rs = await EventApi.getSimpleList()
      if (rs.code === 200) {
        console.log('[ 事件分类-list ] >', rs.payload)
        this.typeList = rs.payload
      }
    },
    setValue(val) {
      if (val === '' || /^\w+$/.test(val)) {
        this.eventData['propertyKey'] = val
      }
    },
    handleConfirm() {
      if(this.isDisable) {
        this.$emit("confirm-edit", this.eventData);
      } else {
        this.$emit("confirm", this.eventData);
      }

      this.setPushEventEvent(this.eventData)
      this.handleCancel()
    },
    handleCancel() {
      this.$emit("cancel");
    },
  },
};
</script>

<style lang="scss" scoped>
:deep(.el-drawer) {
  padding: 0 20px;
  line-height: 1;
}

.el-tag {
  margin-right: 10px;
  cursor: pointer;
}

.tag-active {
  color: red;
}

.form-footer {
  position: absolute;
  right: 40px;
  bottom: 40px;
}

.el-input {
  width: 240px;
}
</style>
